var list="1234567890".split(""), sum=0;
function swap(i,j) {
    tmp = list[i];
    list[i] = list[j];
    list[j] = tmp;
}

function getNumber(list,i,j,k) {
    return parseInt(list[i],10)*100+ parseInt(list[j],10)*10 + parseInt(list[k],10);
}

function test(list) {
    var primes = [2, 3, 5, 7, 11, 13, 17];
    for(var i=0; i<primes.length; i++) {
        var d = getNumber(list, i+1, i+2, i+3);
        if(d%primes[i]!=0) return false;
    }
    return true;
}

function perm(k,m) {
    if(k>m) {
        if(test(list)) {
            sum = sum + (parseInt(list.join(""), 10));
            print(list.join(""));
        }
    } else {
        var pass=true;
        var primes = [2, 3, 5, 7, 11, 13, 17];
        for(var i=0; i<primes.length; i++) {
            if(k>i+3) {
                var d = getNumber(list, i+1, i+2, i+3);
                if(d%primes[i]!=0) pass = false;
            }
        }
        if(pass) {
            for(var i=k; i<=m; i++) {
                swap(k, i);
                perm(k+1, m);
                swap(k, i);
            }
        }
    }
}

print('get primes.....');
var t=new Date().getTime();
perm(0,9);
print(sum);
print('get primes done..... ' + ((new Date().getTime()-t)/1000) + "s");
